package ui
{
	import Common.RenderManager;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class AlartUIHandler
	{
		private var movie:uiMovie = null;
		private var uiMc:MovieClip = null;
		
		private var yesFunction:Function = null;
		private var noFunction:Function = null;
		
		
		public function AlartUIHandler()
		{
			
		}
		
		public function update( delay:Number ):void
		{
		
		}
		
		public function init( ui:MovieClip ):void
		{
			uiMc = ui;
			uiMc.visible = false;
			uiMc.yesBtn.addEventListener( MouseEvent.CLICK , onYes );
			uiMc.noBtn.addEventListener( MouseEvent.CLICK , onNo );
			
			movie = new uiMovie();
			movie.init( uiMc );
			
			RenderManager.instance.UiLoadingSprite.addChild( uiMc );
			
			resize();
		}
		
		public function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = RenderManager.instance.stageWidth * 0.5;
				uiMc.y = RenderManager.instance.stageHeight * 0.5;
			}
			
		}
		
		public function onYes( e:MouseEvent ):void
		{
			if ( yesFunction != null )
			{
				yesFunction();
			}
			
			visible( false );
		}
		
		public function onNo( e:MouseEvent ):void
		{
			if ( noFunction != null )
			{
				noFunction();
			}
			
			visible( false );
		}
		
		public function isVisible():Boolean
		{
			if ( uiMc )
			{
				return uiMc.visible;
			}
			
			return false;
		}
		
		public function show( f1:Function = null , f2:Function = null ):void
		{
			yesFunction = f1;
			noFunction = f2;
			
			visible( true );
		}
		
		public function visible( b:Boolean ):void
		{
			if ( b )
			{
				if ( uiMc.visible )
				{
					return;
				}
				
				uiMc.x = RenderManager.instance.stage.width * 0.5;
				uiMc.y = RenderManager.instance.stage.height * 0.5;
				
				RenderManager.instance.UiLoadingSprite.mouseEnabled = true;
				
				movie.startTimer();
			}
			else
			{
				uiMc.visible = false;
				
				RenderManager.instance.UiLoadingSprite.mouseEnabled = false;
			}
		}
		
		public static var instance:AlartUIHandler = null;
		public static function Instance():AlartUIHandler
		{
			if ( instance == null )
			{
				instance = new AlartUIHandler();
			}
			
			return instance;
		}
		
	}
	
}


